﻿// // --------------------------------------------------------------------------------------------------------------------
// // <copyright file="SPTFeaturePage.cs" company="Collaboris">
// //   Copyright Collaboris
// // </copyright>
// // <summary>
// //   
// // </summary>
// // --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.ShareTest.SharePoint2010.PageObjects.AppPages
{
    #region

    using Collaboris.ShareTest.Core;

    using OpenQA.Selenium;
    using OpenQA.Selenium.Remote;

    #endregion

    /// <summary>
    /// 	Defines a standard SharePoint Features page
    /// </summary>
    public class SPTFeaturePage : SPTPageObjectBase
    {
        #region Constructors and Destructors

        public SPTFeaturePage(RemoteWebDriver driver, string fullUrl)
            : base(driver, fullUrl)
        {
        }

        public SPTFeaturePage(RemoteWebDriver driver, string fullUrl, string verifyElementOnLoad)
            : base(driver, fullUrl, verifyElementOnLoad)
        {
        }

        public SPTFeaturePage(RemoteWebDriver driver, string fullUrl, string verifyElementOnLoad, string loginFileName)
            : base(driver, fullUrl, verifyElementOnLoad, loginFileName)
        {
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// 	Activates or deavitates a feature based on its previous state.
        /// </summary>
        /// <remarks>
        /// 	This does the same as goign tot he features page and clicking the 
        /// 	button.
        /// </remarks>
        /// <param name = "featureId">
        /// </param>
        /// <returns>
        /// 	An instance of the SPTFeaturePage ready to use
        /// 	in chained calls.
        /// </returns>
        public SPTFeaturePage ToggleFeatureState(string featureId)
        {
            Logger.LogMessage("Toggling feature state for feature Id : " + featureId);

            string xpath = "//div[@id='" + featureId + "']/input";
            By locator = By.XPath(xpath);

            IWebElement featureButtonElement = this.webDriver.FindElement(locator);

            if (featureButtonElement.GetAttribute("value") == Common.Map.Features.DeactivateButtonText)
            {
                Logger.LogMessage("Deactivating Feature");

                // click deactivate...and wait...
                this.webDriver.ClickWithWait(locator);

                // wait for element not present
                By deactivatorLocation = By.Id(Common.Map.Features.DeactivateButtonId);

                this.webDriver.ClickWithWait(deactivatorLocation);
            }
            else
            {
                Logger.LogMessage("Activating Feature");

                this.webDriver.ClickWithWait(locator);
            }

            return this;
        }

        #endregion
    }
}